// 普通电话验证(包括手机和电话)
export const ruleTel = value => {
    if (value === '') {
        return false
    } else {
        if (/^(\d){12}$/.test(value)) {
            return true
        } else {
            return false
        }
    }
}
// element ui 电话验证(包括手机和电话)
export const validateTel = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请输入手机号'))
    } else {
        if (/^(\d){12}$/.test(value)) {
            callback()
        } else {
            callback(new Error('请输入正确的手机号'))
        }
    }
}
export const NovalidateTel = (rule, value, callback) => {
    if (value === '') {
        callback()
    } else {
        if (/^(\d){12}$/.test(value)) {
            callback()
        } else {
            callback(new Error('请输入正确的手机号'))
        }
    }
}
/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
    return /^(https?:|mailto:|tel:)/.test(path)
}

// 身份证验证
export const validateIdCode = value => {
    var weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
    var check_code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']

    var code = value + ''
    var last = value[17]

    var seventeen = code.substring(0, 17)

    var arr = seventeen.split('')
    var len = arr.length
    var num = 0
    for (var i = 0; i < len; i++) {
        num = num + arr[i] * weight_factor[i]
    }

    var resisue = num % 11
    var last_no = check_code[resisue]
    var idcard_patter = /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/

    var format = idcard_patter.test(value)
    return !!((last === last_no) & format)
}

// 根据 身份证判断年龄
export const discriCard = value => {
    var UUserCard = value
    UUserCard.substring(6, 10) + '-' + UUserCard.substring(10, 12) + '-' + UUserCard.substring(12, 14)
    var myDate = new Date()
    var month = myDate.getMonth() + 1
    var day = myDate.getDate()
    var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1
    if (
        UUserCard.substring(10, 12) < month ||
        (UUserCard.substring(10, 12) === month && UUserCard.substring(12, 14) <= day)
    ) {
        age++
    }
    return age
}

// 验证手机号
export const CheckPhone = (rule, value, callback) => {
    // 1[345678]\d{9} 号码为数字1开头+一位3-8的数字+后面任意9位数字
    if (!value) {
        callback(new Error('请输入'))
    } else if (!/^1[345678]\d{9}$/.test(value)) {
        callback(new Error('请输入正确的手机号'))
    } else {
        callback()
    }
}

// 日期格式化
export const dateFormat = (fmt, date) => {
    let ret
    const opt = {
        'y+': date.getFullYear().toString(), // 年
        'M+': (date.getMonth() + 1).toString(), // 月
        'd+': date.getDate().toString(), // 日
        'H+': date.getHours().toString(), // 时
        'm+': date.getMinutes().toString(), // 分
        's+': date.getSeconds().toString() // 秒
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
    }
    for (let k in opt) {
        ret = new RegExp('(' + k + ')').exec(fmt)
        if (ret) {
            fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0'))
        }
    }
    return fmt
}
/**
 * 获取上一个月
 * @date 格式为yyyy-mm-dd的日期，如：2014-01-25
 */
export const getPreMonth = time => {
    var days = new Date(time)
    var year2 = days.getFullYear()
    var month2 = days.getMonth()
    if (month2 == 0) {
        year2 = parseInt(year2) - 1
        month2 = 12
    }
    if (month2 < 10) {
        month2 = '0' + month2
    }
    var t2 = year2 + '-' + month2
    return t2
}

/**
 * 获取下一个月
 * @date 格式为yyyy-mm-dd的日期，如：2014-01-25
 */
export const getNextMonth = time => {
    var days = new Date(time)
    var year2 = days.getFullYear()
    var month2 = days.getMonth() + 2
    if (month2 == 13) {
        year2 = parseInt(year2) + 1
        month2 = 1
    }
    if (month2 < 10) {
        month2 = '0' + month2
    }
    var t2 = year2 + '-' + month2
    return t2
}